<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Application;
use App\Models\LaborContract;
use App\Models\MaterialContract;
use App\Models\Project;
use App\Models\ProjectCost;
use App\Models\insert;
use App\Models\ProjectUser;
use App\Models\ReimburseDetail;
use Illuminate\Http\Request;

class StatisticsController extends Controller
{
    public function index(Request $Request) {
        $data = [];
        $project = Project::find($Request->project_id);
        $arr = [
            '合同金额（一般计税）','审定金额（一般计税）','一、直接成本：（不含进项税）',
            '（一）人工费：','单包人工',
            '（二）、材料费：','材料采购','双包',
            '（三）管理人员工资 ：','管理人员费用',
            '（四）费用支付：','差旅费','房屋、设备租赁费','办公费用及其他','福利费','业务招待费','汽车费用','工程保险','效果图外协','运输费','保函手续费','材料检测费','固定资产',
            '（五）费用分摊','费用分摊','二、后期费用（预估）',
            '（一）审计费用','（二）维修费用','（三）收款费用','（四）总包费用','（五）其他费用',
            '三、税金计算','税金费用',
            '四、公司管理费','（一）提留比率（10%）',
            '（二）计提金额','五、总成本合计','六、项目利润','七、毛利率'
        ];

        $insert = [];
        foreach ($arr as $k => $v) {
            $insert[$k]['title'] = $v;
            $insert[$k]['project_id'] = $Request->project_id;
            $insert[$k]['price'] = 0;
            $insert[$k]['title'] = $v;
            $insert[$k]['remarks'] = '/';
        }
        $cost = ProjectCost::where('project_id',$Request->project_id)->sum('total') + Application::where('status','approved')->where('project_id',$Request->project_id)->sum('price');
        $ReimburseID = Application::where('model','Reimburse')->where('project_id',$Request->project_id)->where('status','approved')->pluck('source_id');
        $ReimburseDetail = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->get();
        foreach ($insert as $k => $v) {
            if($v['title'] == '合同金额（一般计税）'){
                $insert[$k]['price'] = $project->price;
            }
            if($v['title'] == '审定金额（一般计税）'){
                $insert[$k]['price'] = $project->price;
            }
            if($v['title'] == '一、直接成本：（不含进项税）'){
                $insert[$k]['price'] = Application::where('status','approved')->where('project_id',$Request->project_id)->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }
            if($v['title'] == '（一）人工费：'){
                $insert[$k]['price'] = Application::where('source','LaborContract')->where('project_id',$Request->project_id)->where('status','approved')->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }
            if($v['title'] == '单包人工'){
                $insert[$k]['price'] = Application::where('source','LaborContract')->where('project_id',$Request->project_id)->where('status','approved')->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }
            if($v['title'] == '（二）、材料费：'){
                $insert[$k]['price'] = Application::where('source',['MaterialContract','MaterialContractDouble'])->where('project_id',$Request->project_id)->where('status','approved')->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }
            if($v['title'] == '材料采购'){
                $insert[$k]['price'] = Application::where('source','MaterialContract')->where('project_id',$Request->project_id)->where('status','approved')->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }
            if($v['title'] == '双包'){
                $insert[$k]['price'] = Application::where('source','MaterialContractDouble')->where('project_id',$Request->project_id)->where('status','approved')->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }
            if($v['title'] == '（三）管理人员工资'){
                $insert[$k]['price'] = ProjectUser::where('project_id',$Request->project_id)->where('status','approved')->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }
            if($v['title'] == '管理人员费用'){
                $insert[$k]['price'] = ProjectUser::where('project_id',$Request->project_id)->where('status','approved')->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '（四）费用支付：'){
                $insert[$k]['price'] = Application::where('model','Reimburse')->where('project_id',$Request->project_id)->where('status','approved')->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '差旅费'){
                $insert[$k]['price'] = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '房屋、设备租赁费'){
                $insert[$k]['price'] = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '办公费用及其他'){
                $insert[$k]['price'] = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '福利费'){
                $insert[$k]['price'] = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '业务招待费'){
                $insert[$k]['price'] = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
                
            }

            if($v['title'] == '汽车费用'){
                $insert[$k]['price'] = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '工程保险'){
                $insert[$k]['price'] = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '效果图外协'){
                $insert[$k]['price'] = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '运输费'){
                $insert[$k]['price'] = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '保函手续费'){
                $insert[$k]['price'] = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }
            if($v['title'] == '材料检测费'){
                $insert[$k]['price'] = ReimburseDetail::whereIn('reimburse_id',$ReimburseID)->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }
            if($v['title'] == '固定资产'){
                $insert[$k]['price'] = $ReimburseDetail->where('title',$v['title'])->sum('price');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '（五）费用分摊'){
                $insert[$k]['price'] = ProjectCost::where('type','sharing')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '费用分摊'){
                $insert[$k]['price'] = ProjectCost::where('type','sharing')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '二、后期费用（预估）'){
                $insert[$k]['price'] = ProjectCost::where('type','subsequent')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '（一）审计费用'){//
                $insert[$k]['price'] = ProjectCost::where('title','审计费用')->where('type','subsequent')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '（二）维修费用'){
                $insert[$k]['price'] = ProjectCost::where('title','维修费用')->where('type','subsequent')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '（三）收款费用'){
                $insert[$k]['price'] = ProjectCost::where('title','收款费用')->where('type','subsequent')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '（四）总包费用'){
                $insert[$k]['price'] = ProjectCost::where('title','总包费用')->where('type','subsequent')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '（五）其他费用'){
                $insert[$k]['price'] = ProjectCost::where('title','其他费用')->where('type','subsequent')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '三、税金计算'){
                $insert[$k]['price'] = ProjectCost::where('type','taxes')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '税金费用'){
                $insert[$k]['price'] = ProjectCost::where('type','taxes')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '四、公司管理费'){
                $insert[$k]['price'] = ProjectCost::where('type','company')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '（一）提留比率（10%）'){
                $insert[$k]['price'] = ProjectCost::where('title','提留比率（合同金额10%比例计入）')->where('type','company')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '（二）计提金额'){
                $insert[$k]['price'] = ProjectCost::where('title','计提金额')->where('type','company')->where('project_id',$Request->project_id)->sum('total');
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '五、总成本合计'){
                $insert[$k]['price'] = $cost;
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }


            if($v['title'] == '六、项目利润'){
                $insert[$k]['price'] = $project->price - $cost;
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }

            if($v['title'] == '七、毛利率'){
                $insert[$k]['price'] = $project->price - $cost;
                $insert[$k]['proportion'] = number_format($insert[$k]['price'] / $project->price * 100,2).'%';
            }
        }
        
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$insert]);
    }

    public function overview(Request $Request) {
        $data = [];
        $project = Project::find($Request->project_id);
        $cost = ProjectCost::where('project_id',$Request->project_id)->sum('total') + Application::where('status','approved')->where('project_id',$Request->project_id)->sum('price');
        $invoice = Application::where('model','ProjectInvoice')->where('project_id',$Request->project_id)->where('status','approved')->sum('price');
        $pay = Application::where('project_id',$Request->project_id)->where('status','approved')->sum('price');
        $data['price'] = $project->price;
        $data['gradation_price'] = $project->gradation_price;
        $data['examine_price'] = $project->examine_price;
        $data['invoice'] = $invoice;
        $data['account_price'] = $project->account_price;
        $data['pay'] = $pay;
        $data['balance_price'] = $project->balance_price;
        $data['no_pay_price'] = $project->no_pay_price;
        $data['cost'] = $cost;
        $data['profit'] = $project->price - $cost;
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function costs(Request $Request) {
        $MaterialContractID = Application::where('model','MaterialContract')->where('project_id',$Request->project_id)->where('status','approved')->pluck('source_id');
        $LaborContractID = Application::where('model','LaborContract')->where('project_id',$Request->project_id)->where('status','approved')->pluck('source_id');
 
        $MaterialContract = MaterialContract::with('reimburses')->where('contract_type',1)->whereIn('id',$MaterialContractID)->get();
        $MaterialDoubleContract = MaterialContract::with('reimburses')->where('contract_type',3)->whereIn('id',$MaterialContractID)->get();
        $LaborContract = LaborContract::with('reimburses')->whereIn('id',$LaborContractID)->get();
        //已支付金额
        foreach ($MaterialContract as $k => $v) {
            $MaterialContract[$k]->pay_price = 0;
            foreach ($v->reimburses as $key => $value) {
                if($value->applicationApproved){
                    $MaterialContract[$k]->pay_price += $value->applicationApproved->price;
                }
                
            }
            $MaterialContract[$k]->pay_ratio = round(($MaterialContract[$k]->pay_price / $v->price) * 100,2).'%';
            $MaterialContract[$k]->tax_deduction_cost = $v->price - $v->tax_price;
        }

        //已支付金额
        foreach ($MaterialDoubleContract as $k => $v) {
            $MaterialDoubleContract[$k]->pay_price = 0;
            foreach ($v->reimburses as $key => $value) {
                if($value->applicationApproved){
                    $MaterialDoubleContract[$k]->pay_price += $value->applicationApproved->price;
                }
            }
            $MaterialDoubleContract[$k]->pay_ratio = round(($MaterialDoubleContract[$k]->pay_price / $v->price) * 100,2).'%';
            $MaterialDoubleContract[$k]->tax_deduction_cost = $v->price - $v->tax_price;
        }

        //已支付金额
        foreach ($LaborContract as $k => $v) {
            $LaborContract[$k]->pay_price = 0;
            foreach ($v->reimburses as $key => $value) {
                if($value->applicationApproved){
                    $LaborContract[$k]->pay_price += $value->applicationApproved->price;
                }
            }
            $LaborContract[$k]->pay_ratio = round(($LaborContract[$k]->pay_price / $v->price) * 100,2).'%';
            $LaborContract[$k]->tax_deduction_cost = $v->price - $v->tax_price;
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','MaterialContract'=>$MaterialContract,'MaterialDoubleContract'=>$MaterialDoubleContract,'LaborContract'=>$LaborContract]);
    }

    public function financeCosts(Request $Request) {
        $data = [];
        $data['MaterialPay'] = Application::where('source','MaterialPay')->where('project_id',$Request->project_id)->where('status','approved')->sum('price');
        $data['LaborPay']  = Application::where('source','LaborPay')->where('project_id',$Request->project_id)->where('status','approved')->sum('price');
        $data['Reimburse']  = Application::where('source','Reimburse')->where('project_id',$Request->project_id)->where('status','approved')->sum('price');
       
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }
}
